import datetime
import functools
import logging

import experimental.prophet as prophet
from absl import app
from experimental.prophet.ops import timeseries

from coin.exchange.bitmex.kr_rest.futures_product import BitmexFuturesProduct
from coin.exchange.okex_futures.kr_rest.futures_product import OkexFuturesProduct
from coin.exchange.upbit_v1.kr_rest.product import UpbitProduct


def model(coin, past_mins, future_mins):
  ts = prophet.global_timestamp()
  timer_1s = prophet.timer('1s')

  # product = OkexFuturesProduct.FromStr('%s-USD.QUARTER' % coin)

  # from coin.exchange.bitmex.kr_rest.product_util import gen_bitmex_futures_product_list
  # print(gen_bitmex_futures_product_list())
  product = BitmexFuturesProduct.FromStrProduct('XRP-BTC.20190927')

  feed = prophet.fastfeed_coin(product, 'Futures.Bitmex')
  book = prophet.get_book_from_fastfeed(feed)
  price = (book.ask0.price + book.bid0.price) / 2.

  instrument = prophet.get_instrument_from_fastfeed(feed)

  # open_interest_now = timeseries.time_shift(instrument.open_interest, str(future_mins) + 'm')
  # open_interest_past = timeseries.time_shift(instrument.open_interest,
  #                                            str(past_mins + future_mins) + 'm')
  # price_fut = price
  # price_now = timeseries.time_shift(price, str(future_mins) + 'm')
  # price_past = timeseries.time_shift(price, str(past_mins + future_mins) + 'm')

  # past_interest_diff = (open_interest_now - open_interest_past).named_ref('past_open_interest_diff')
  # future_return = (price_fut / price_now - 1.0).named_ref('future_return')
  # past_return = (price_now / price_past - 1.0).named_ref('past_return')

  with prophet.control_if(ts % 10**9 == 0):
    prophet.printer('%t %s %s %s', ts, instrument.funding_rate, instrument.mark_price)
    # aggregator = prophet.scalar_value_aggregator([ts, instrument.open_interest])
    # prophet.printer('%t --- past interest diff %s future_return %s', ts, past_interest_diff,
    #                 future_return)
    # aggregator = prophet.scalar_value_aggregator([ts, past_interest_diff, past_return])

    # aggregator = prophet.scalar_value_aggregator([ts, past_interest_diff, future_return])
    aggregator = prophet.scalar_value_aggregator([ts, price])

  return aggregator


def main(argv):
  # Bitmex/OkexFutures
  coin = 'XBT'
  mx = 0
  for past_mins in [3]:
    for future_mins in [3]:
      df = prophet.run_from_fastfeed_multiprocess(functools.partial(model,
                                                                    coin,
                                                                    past_mins,
                                                                    future_mins),
                                                  datetime.date(2019, 7, 1),
                                                  datetime.datetime(2019, 7, 1, 0, 1),
                                                  machine='feed-01.ap-northeast-1.aws',
                                                  use_run_cache=False)

      # df = df[df.past_open_interest_diff < 50000][df.past_open_interest_diff > -50000]
      # df = df.abs()

      # corr = df.corr().iloc[1][2]
      # print(f'past_mins {past_mins} future_mins {future_mins} corr {corr:10.5f}')

      # d = abs(corr)
      # if d > mx:
      #   mx = d

      # if past_mins == 3 and future_mins == 3:
      #   chart = alt.Chart(df).mark_circle(size=2).encode(x='past_open_interest_diff',
      #                                                    y='future_return')
      #   chart.save('chart.html')

  print('mx: ', mx)


if __name__ == '__main__':
  logging.basicConfig(level='DEBUG')
  app.run(main)
